﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eoffice.Models;
using System.IO;

namespace Eoffice.Controllers
{
    public class CvDenController : Controller
    {
        private EofficeDb db = new EofficeDb();
        static string key = "";
        static string linhvuc = "";
        static string loai = "";
        //
        // GET: /CvDen/

        public ActionResult Index()
        {

            //var cvdens = db.CVDENs.Include(c => c.CQNGOAI).Include(c => c.LOAICV).Include(c => c.NV).Include(c => c.LINHVUC);
            //return View(cvdens.ToList());

            //if (Session["Username"] != null)

            

            if (Session["KEY"] != null) { key = Session["KEY"].ToString(); }

            var trichyeu = (from p in db.CVDENs where p.TRICHYEUCVDEN.Contains(key) select p).ToList();
            var socv = (from p in db.CVDENs where p.SOCV.Contains(key) select p).ToList();
            if (key == "")
            {                
                string page = "1"; //so phan trang hien tai
                var pagesize = 5; //so ban ghi tren 1 trang
                var numOfNews = 0; //tong so ban ghi co duoc truoc khi phan trang
                int curpage = 0; // trang hien tai dung cho phan trang
                if (Request["page"] != null)
                {
                    page = Request["page"];
                    curpage = Convert.ToInt32(page) - 1;
                }
                var all = db.CVDENs.ToList();

                var pages = all.Skip(curpage * pagesize).Take(pagesize).ToList();

                //var pages = data.sp_Brands_Phantrang(page, productize, "", "").ToList();
                var url = Request.Path;
                numOfNews = all.Count;

                if (numOfNews > 0)
                {
                    ViewBag.Pager = Phantrang.PhanTrang(pagesize, curpage, numOfNews, url);
                }
                else
                {
                    ViewBag.Pager = "";
                }

                return View(pages);
            }
            else if (trichyeu.Count > 0)
            {
                string page = "1"; //so phan trang hien tai
                var pagesize = 5; //so ban ghi tren 1 trang
                var numOfNews = 0; //tong so ban ghi co duoc truoc khi phan trang
                int curpage = 0; // trang hien tai dung cho phan trang
                if (Request["page"] != null)
                {
                    page = Request["page"];
                    curpage = Convert.ToInt32(page) - 1;
                }
                var all = trichyeu;

                var pages = all.Skip(curpage * pagesize).Take(pagesize).ToList();

                //var pages = data.sp_Brands_Phantrang(page, productize, "", "").ToList();
                var url = Request.Path;
                numOfNews = all.Count;

                if (numOfNews > 0)
                {
                    ViewBag.Pager = Phantrang.PhanTrang(pagesize, curpage, numOfNews, url);
                }
                else
                {
                    ViewBag.Pager = "";
                }

                return View(pages);
            }
            else if (socv.Count > 0)
            {
                string page = "1"; //so phan trang hien tai
                var pagesize = 5; //so ban ghi tren 1 trang
                var numOfNews = 0; //tong so ban ghi co duoc truoc khi phan trang
                int curpage = 0; // trang hien tai dung cho phan trang
                if (Request["page"] != null)
                {
                    page = Request["page"];
                    curpage = Convert.ToInt32(page) - 1;
                }
                var all = socv;

                var pages = all.Skip(curpage * pagesize).Take(pagesize).ToList();

                //var pages = data.sp_Brands_Phantrang(page, productize, "", "").ToList();
                var url = Request.Path;
                numOfNews = all.Count;

                if (numOfNews > 0)
                {
                    ViewBag.Pager = Phantrang.PhanTrang(pagesize, curpage, numOfNews, url);
                }
                else
                {
                    ViewBag.Pager = "";
                }

                return View(pages);
            }
            else
                return View(trichyeu);

        }

        [HttpPost]
        public ActionResult Index(FormCollection conllect)
        {
            key = "";
            key = conllect["txtSearch"];
            Session["KEY"] = key;

            return RedirectToAction("Index");
        }

        #region[Search]
        [ChildActionOnly]
        public ActionResult Search()
        {
            var linhvuc = db.LINHVUCs.ToList();
            for (int i = 0; i < linhvuc.Count; i++)
            {
                ViewBag.linhvuc = new SelectList(linhvuc, "MALV", "TENLV");
            }
            var loai = db.LOAICVs.ToList();
            for (int i = 0; i < loai.Count; i++)
            {
                ViewBag.loai = new SelectList(loai, "MALOAI", "TENLOAI");
            }


            return PartialView();
        }
        #endregion
        #region[Search]
        [HttpPost]
        public ActionResult Search(FormCollection collect)
        {
            linhvuc = "";
            linhvuc = collect["linhvuc"];
            Session["LINHVUC"] = linhvuc;

            loai = "";
            loai = collect["loai"];
            Session["LOAI"] = loai;

            return RedirectToAction("Index");
        }
        #endregion
        

        //
        // GET: /CvDen/Details/5

        public ActionResult Details(int id)
        {
            CVDEN cvden = db.CVDENs.First(cv => cv.STTCV == id);
            if (cvden == null)
            {
                return HttpNotFound();
            }
            string chuoi = "";
            chuoi += cvden.FILE;
            ViewBag.File = chuoi;

            var lq = db.LIENQUANs.Where(m => m.STTCV == id).ToList();
            for (int i = 0; i < lq.Count; i++)
            {
                ViewBag.lq += lq[i].SOCVDI + ",";
            }

            var xl = db.VIEW_DENXULY.Where(m => m.STT == id).ToList();
            for (int i = 0; i < xl.Count; i++)
            {
                ViewBag.nvxl += xl[i].Họ_tên_NV + ",";
                ViewBag.ndxl += xl[i].Nội_dung_xử_lý;
            }

            ViewBag.MALOAI = new SelectList(db.LOAICVs, "MALOAI", "TENLOAI", cvden.MALOAI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", cvden.MSNV);
            ViewBag.MALV = new SelectList(db.LINHVUCs, "MALV", "TENLV", cvden.MALV);

            return View(cvden);
        }

        //
        // GET: /CvDen/Create

        public ActionResult Create()
        {
            ViewBag.MACQ = new SelectList(db.CQNGOAIs, "MACQ", "TENCQ");
            ViewBag.MALOAI = new SelectList(db.LOAICVs, "MALOAI", "TENLOAI");
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV");
            ViewBag.MALV = new SelectList(db.LINHVUCs, "MALV", "TENLV");

            return View();
        }

        //
        // POST: /CvDen/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CVDEN cvden, FormCollection form)
        {
            CVDEN cvd = db.CVDENs.OrderByDescending(r => r.STTCV).First();

            //cvden.STTCV = Convert.ToInt32(form["STTCV"]);
            cvden.STTCV = cvd.STTCV + 1;
            cvden.NGAYCVDEN = form["NGAYCVDEN"].ToString();
            string socv = form["SOCV"].ToString();
            cvden.SOCV = socv.ToUpper();
            cvden.MALV = form["MALV"].ToString();
            cvden.MALOAI = form["MALOAI"].ToString();
            cvden.MSNV = form["MSNV"].ToString();
            cvden.MACQ = form["MACQ"].ToString();
            cvden.NGAYVAOSO = form["NGAYVAOSO"].ToString();

            //var ngayvaoso = DateTime.Now;
            //cvden.NGAYVAOSO = ngayvaoso.ToString();

            cvden.NGUOIKY = form["NGUOIKY"].ToString();
            cvden.GHICHU = form["GHICHU"].ToString();
            cvden.FILE = form["FILE"].ToString();

            string trichyeu = form["TRICHYEUCVDEN"].ToString();
            cvden.TRICHYEUCVDEN = trichyeu;

            if (trichyeu.Length > 50)
            {
                cvden.Trichdan = trichyeu.Substring(0, 50) + "...";
            }
            else
                cvden.Trichdan = trichyeu;

            cvden.Tinhtrang = 0;

            ViewBag.MACQ = new SelectList(db.CQNGOAIs, "MACQ", "TENCQ", cvden.MACQ);
            ViewBag.MALOAI = new SelectList(db.LOAICVs, "MALOAI", "TENLOAI", cvden.MALOAI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", cvden.MSNV);
            ViewBag.MALV = new SelectList(db.LINHVUCs, "MALV", "TENLV", cvden.MALV);

            db.CVDENs.Add(cvden);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /CvDen/Edit/5

        public ActionResult Edit(int id)
        {
            CVDEN cvden = db.CVDENs.Find(id);
            if (cvden == null)
            {
                return HttpNotFound();
            }
            ViewBag.MACQ = new SelectList(db.CQNGOAIs, "MACQ", "TENCQ", cvden.MACQ);
            ViewBag.MALOAI = new SelectList(db.LOAICVs, "MALOAI", "TENLOAI", cvden.MALOAI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", cvden.MSNV);
            ViewBag.MALV = new SelectList(db.LINHVUCs, "MALV", "TENLV", cvden.MALV);
            return View(cvden);
        }

        //
        // POST: /CvDen/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, FormCollection form)
        {
            CVDEN cvden = db.CVDENs.First(cv => cv.STTCV == id);


            cvden.NGAYCVDEN = form["NGAYCVDEN"].ToString();
            cvden.SOCV = form["SOCV"].ToString();
            cvden.MALV = form["MALV"].ToString();
            cvden.MALOAI = form["MALOAI"].ToString();
            cvden.MSNV = form["MSNV"].ToString();
            cvden.MACQ = form["MACQ"].ToString();
            cvden.NGAYVAOSO = form["NGAYVAOSO"].ToString();
            cvden.NGUOIKY = form["NGUOIKY"].ToString();
            cvden.GHICHU = form["GHICHU"].ToString();
            cvden.FILE = form["FILE"].ToString();
            string trichyeu = form["TRICHYEUCVDEN"].ToString();
            cvden.TRICHYEUCVDEN = trichyeu;

            if (trichyeu.Length > 50)
            {
                cvden.Trichdan = trichyeu.Substring(0, 50) + "...";
            }
            else
                cvden.Trichdan = trichyeu;

            ViewBag.MACQ = new SelectList(db.CQNGOAIs, "MACQ", "TENCQ", cvden.MACQ);
            ViewBag.MALOAI = new SelectList(db.LOAICVs, "MALOAI", "TENLOAI", cvden.MALOAI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", cvden.MSNV);
            ViewBag.MALV = new SelectList(db.LINHVUCs, "MALV", "TENLV", cvden.MALV);

            db.SaveChanges();

            return RedirectToAction("Index");
        }
        //
        public ActionResult _search()
        {
            string chuoi = "";
            var phongban = db.PHONGBANs.ToList();
            var linhvuc = db.LINHVUCs.ToList();
            var loai = db.LOAICVs.ToList();
            var nhanvien = db.NVs.ToList();
            chuoi += "<select id='TenNv' name='TenNv'>";
            chuoi += "<option value>=== Chọn tên nhân viên ===</option>";
            //for (int i = 0; i < phongban.Count; i++)
            //{
            //    var mem = db.NVs.Where(m => m.MAPB == phongban[i].MAPB).ToList();
            //    if (mem.Count > 0)
            //    {
            //        chuoi += "<optgroup label=\"" + phongban[i].TENPB + "\"></optgroup>";
            //        for (int k = 0; k < mem.Count; k++)
            //        {
            //            chuoi += "<option value='" + mem[k].MSNV + "'>" + mem[k].HOTENNV + "</option>";
            //        }
            //    }
            //}
            //chuoi += "</select>";
            for (int i = 0; i < phongban.Count; i++)
            {
                ViewBag.phongban = new SelectList(phongban, "MAPB", "TENPB");
            }
            for (int i = 0; i < linhvuc.Count; i++)
            {
                ViewBag.linhvuc = new SelectList(linhvuc, "MALV", "TENLV");
            }
            for (int i = 0; i < loai.Count; i++)
            {
                ViewBag.loai = new SelectList(loai, "MALOAI", "TENLOAI");
            }
            for (int i = 0; i < nhanvien.Count; i++)
            {
                ViewBag.nhanvien = new SelectList(nhanvien, "MSNV", "HOTENNV");
            }
            ViewBag.TenNv = chuoi;

            return PartialView();
        }

        [HttpPost]
        public ActionResult _search(FormCollection collect)
        {
            var DateBegin = "";
            var DateEnd = "";
            var Phongban = "";
            var Nv = "";
            var LinhvucCv = "";
            var LoaiCv = "";
            var CqNgoai = "";
            DateBegin = collect["dateBegin"];
            DateEnd = collect["dateEnd"];
            Phongban = collect["Phongban"];
            Nv = collect["nhanvien"];
            LinhvucCv = collect["linhvuc"];
            LoaiCv = collect["loai"];
            CqNgoai = collect["CqNgoai"];
            var url = "/CvDen/Index?dateForm=" + DateBegin + "&dateTo=" + DateEnd + "&room=" + Phongban + "&MSNV=" + Nv + "&LinhvucCv=" + LinhvucCv + "&LoaiCv=" + LoaiCv + "";
            return Redirect(url);
        }

        public ActionResult _timkiem()
        {
            return PartialView();
        }


        public ActionResult SelectNV()
        {
            return View();
        }

        //
        // GET: /CvDen/Delete/5

        public ActionResult Delete(int id)
        {
            CVDEN cvden = db.CVDENs.Find(id);
            if (cvden == null)
            {
                return HttpNotFound();
            }
            return View(cvden);
        }

        //
        // POST: /CvDen/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CVDEN cvden = db.CVDENs.Find(id);
            db.CVDENs.Remove(cvden);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}